import { GrowlMessageType } from './growMeaageType';
import { MessageBoxContainer } from './MessageBoxContainer';
import { ConfirmMessageResult } from "./confirmMessage";

export class MessageBoxService {
    constructor() {

    }
    static current: MessageBoxService = new MessageBoxService()
    private container: MessageBoxContainer
    Regist(container: MessageBoxContainer) {
        this.container = container;
        this.container.GrowlMessage = [];
    }

    Show(title: string, content: string, showOK = true, showCancel = true, showIgnore = true) {
        this.container.Title = title;
        this.container.Content = content;
        this.container.Visible = true;
        this.container.showOK = showOK;
        this.container.showCancel = showCancel;
        this.container.showIgnore = showIgnore;
        return new Promise<ConfirmMessageResult>((resolve, reject) => {
            this.container.onResult = (result) => {
                let r: ConfirmMessageResult;
                switch (result) {
                    case "ok": r = ConfirmMessageResult.Ok; break;
                    case "cancel": r = ConfirmMessageResult.Cancel; break;
                    case "ignore": r = ConfirmMessageResult.Ignore; break;
                    default: break;
                }
                this.container.Visible = false;
                resolve(r);
            }
        });
    }

    Growl(type: GrowlMessageType, title: string, content?: string) {
        if (this.container.GrowlMessage[this.container.GrowlMessage.length - 1] && this.container.GrowlMessage[this.container.GrowlMessage.length - 1].content == content) {
            return;
        }
        this.container.GrowlMessage.unshift({ severity: type.Name, title: title, content: content });
    }
}





